package com.zq.car.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zq.api.product.ProductFeignClient;
import com.zq.car.dao.dto.CartSaveDto;
import com.zq.car.dao.dto.CartUpdateCheck;
import com.zq.car.dao.entity.CartInfo;
import com.zq.car.dao.entity.ProductSku;
import com.zq.car.mapper.CartInfoMapper;
import com.zq.car.service.ICartInfoService;
import com.zq.common.exception.ResultException;
import com.zq.common.util.JwtUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 购物车信息表 服务实现类
 * </p>
 *
 * @author 张乔
 * @since 2025-06-01
 */
@Service
@Slf4j
public class CartInfoServiceImpl extends ServiceImpl<CartInfoMapper, CartInfo> implements ICartInfoService {
//添加购物车

    @Resource
    private JwtUtil jwtUtil;

    @Resource
    private ProductFeignClient productFeignClient;

    @Override
    public void saveCartInfo(CartSaveDto cartSaveDto,String token) {
        Integer userId = jwtUtil.getUsernameFromToken(token);
        cartSaveDto.setUserId(Long.valueOf(userId));

        CartInfo cartInfo = this.getOne(new LambdaQueryWrapper<CartInfo>().
                eq(cartSaveDto.getUserId() != null, CartInfo::getUserId, cartSaveDto.getUserId())
                .eq(cartSaveDto.getSkuId() != null, CartInfo::getSkuId, cartSaveDto.getSkuId())
        );
        if (cartInfo != null) {
cartInfo.setSkuNum(cartInfo.getSkuNum() + cartSaveDto.getSkuNum());
this.updateById(cartInfo);
        }else {
         cartInfo = new CartInfo();
            String hello = productFeignClient.getHello(50L);
            System.err.println(hello+"这里出现异常");
            // 购物车数据是从商品详情得到 {skuInfo}
//            Object productSkuData = productFeignClient.getBySkuId(cartSaveDto.getSkuId()).getData() ;
            Object productSkuData = productFeignClient.getBySkuId(cartSaveDto.getSkuId()).getData() ;
log.info("商品详情数据：{}",productSkuData);
            ProductSku productSku = JSON.parseObject(JSON.toJSONString(productSkuData), ProductSku.class);
//            ProductSku productSku=new ProductSku();
//         BeanUtils.copyProperties(productSkuData,cartInfo);
            log.info("商品详情数据：{}",productSku);
            cartInfo.setCartPrice(productSku.getSalePrice());
            cartInfo.setSkuNum(cartSaveDto.getSkuNum());
            cartInfo.setSkuId(cartSaveDto.getSkuId());
            cartInfo.setUserId(cartSaveDto.getUserId());
            cartInfo.setImgUrl(productSku.getThumbImg());
            cartInfo.setSkuName(productSku.getSkuName());
            cartInfo.setIsChecked(false);
       this.save(cartInfo);



        }


    }

    //查询用户所有购物车
    @Override
    public List<CartInfo> getList(String token) {
        Integer userId = jwtUtil.getUsernameFromToken(token);
        List<CartInfo> cartInfos = this.list(new LambdaQueryWrapper<CartInfo>()
                .eq(userId!=null,CartInfo::getUserId, userId));


        return cartInfos;
    }

    @Override
    public void deleteById(String token,Integer id) {

        Integer userId = jwtUtil.getUsernameFromToken(token);
        this.remove(new LambdaQueryWrapper<CartInfo>()
                .eq(userId!=null, CartInfo::getUserId, userId)
                .eq(id != null, CartInfo::getId, id));
    }

    //修改购物车的选中状态
    @Override
    public void updateSelect(String token, CartUpdateCheck cartUpdateCheck) {
log.info("修改购物车选中状态：{}",cartUpdateCheck);
        CartInfo cartInfo = new CartInfo();
        cartInfo.setIsChecked(cartUpdateCheck.getIsChecked());
        cartInfo.setId(Long.valueOf(cartUpdateCheck.getId()));
        Integer userId = jwtUtil.getUsernameFromToken(token);
        cartInfo.setUserId(Long.valueOf(userId));
  this.updateById(cartInfo);


    }

    @Override
    public List<CartInfo> getAllCkecked(String token) {
        Integer userId = jwtUtil.getUsernameFromToken(token);
        List<CartInfo> cartInfos = this.list(new LambdaQueryWrapper<CartInfo>()
                .eq(userId!=null,CartInfo::getUserId, userId));
        return cartInfos;
    }

    //根据id查询购物车
    @Override
    public CartInfo getByOrderId(Long orderId) {
        CartInfo cartInfo = this.getById(orderId);
        return cartInfo;
    }

    //    获取当前登录用户选中的购物项列表数据并已经勾选了
    @Override
    public List<CartInfo> getAllCkeckedcart(String token) {
        Integer userId = jwtUtil.getUsernameFromToken(token);
        List<CartInfo> cartInfos = this.list(new LambdaQueryWrapper<CartInfo>()
                .eq(userId!=null,CartInfo::getUserId, userId)
                .eq(CartInfo::getIsChecked, true)
        );
        return cartInfos;
    }

    //删除用户已经选中的购物车
    @Override
    public void deleteChecked(String token) {
        Integer userId = jwtUtil.getUsernameFromToken(token);
        if (userId==null){
            throw new ResultException(500,"用户不存在");
        }
        this.remove(new LambdaQueryWrapper<CartInfo>().
                eq(CartInfo::getUserId,userId)
                .eq(CartInfo::getIsChecked,true));



    }


}
